$(document).ready(function() {
	$('input#EmailTextBox').keyup(function() {
		// 验证邮箱是否对且是否已被注册
		var email = $('input#EmailTextBox').val();
		var test  = valideEmail(email);
		if(test){
			valideEmailAjax(email);
		}
	});
	
	var param = $("#action").html().trim();
	$("#"+param).addClass("focus");
	
	$("#PhoneTextBox1").keyup(function() {
		$("#ChangePhoneTips").html('');
		checkPhone($(this).val());
	});
	
});

/* 验证邮箱格式 */
function valideEmail(email){
    if (/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(email)) {
        return true;
    }else if(email=""){
    	return true;
    }else {
    	showEmailTips("你填写的邮箱格式不对");
		return false;
    }
}

/* 验证邮箱是否被注册 */
function valideEmailAjax(email){
	var userName  =  $("#userName").html().trim();
	$.ajax({
		type : "POST",
		url : "servlet/UserServlet",
		data : "action=valide&userName=" +userName+"&email="+email,
		success : function(result) {
			var result= result.trim();
			if(result=="success"){
				// window.location.href="shopCart.jsp";
				$("#EmailTips").html("");
				return true;
			}else{
				showEmailTips("你填写的邮箱已被注册，请重新填写");
				return false;
			}
		}
	});
}

/* 显示邮箱提示 */
function showEmailTips(text){
	$("#EmailTips").html(text);
}

function changeUserInfo(){
	var userName = $("#userName").html().trim();
	var addr = $("#AddressTextBox").val();
	var sex = $("#SexTextBox").val();
	var email =$("#EmailTextBox").val();
	var campus =$("#CampusTextBox").val();
	var other =$("#OtherTextBox").val();
	var emailTips=$("#EmailTips").html().trim();
	if(emailTips!=null&&emailTips!=""){
		$("#ChangeInfoTips").html(emailTips);
		// alert($("#EmailTips").html().trim());
		return false;
	}
	saveUserInfo(userName, addr, sex, email, campus, other);
}

function saveUserInfo(userName,addr,sex,email,campus,other){
	$.ajax({
		type : "POST",
		url : "servlet/UserServlet",
		data : "action=changeInfo&userName=" +userName+"&addr="+addr+"&sex="+sex+"&email="+email+"&campus="+campus+"&other="+other,
		success : function(result) {
			var result= result.trim();
			if(result=="success"){
				$("#ChangeInfoTips").css("color","blue");
				$("#ChangeInfoTips").html("你的信息已保存");
				return true;
			}else{
				$("#ChangeInfoTips").html("信息保存失败");
				return false;
			}
		}
	});
}


/* 修改密码 */
function changePassword(){
	var userName = $("#userName").html().trim();
	var pwd1 = $("#PasswordTextBox1").val();
	var pwd2 = $("#PasswordTextBox2").val();
	if(pwd1==""){
		alert("密码不能为空");
		return false;
	}
	if(pwd1!=pwd2){
		alert("两次输入的密码不一样");
		return false;
	}

	$.ajax({
		type : "POST",
		url : "servlet/UserServlet",
		data : "action=changePassword&userName=" +userName+"&password="+pwd1,
		success : function(result) {
			var result= result.trim();
			if(result=="success"){
				$("#ChangePasswordTips").css("color","blue");
				$("#ChangePasswordTips").html("你的新密码已保存");
				return true;
			}else{
				$("#ChangePasswordTips").html("密码修改失败");
				return false;
			}
		}
	});
}

/* 修改电话 */
function changePhone(){
	var userName = $("#userName").html().trim();
	var phone= $("#PhoneTextBox").html().trim();
	var phone1 = $("#PhoneTextBox1").val();
	if(phone1==""){
		$("#ChangePhoneTips").html("电话不能为空");
		return false;
	}
	if (!(/^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|14[0-9]|15[0|2|3|6|7|8|9]|18[3|6|7|8|9])\d{8}$)/.test(phone1))) {
		$("#ChangePhoneTips").html("电话号码格式不对");
		return false;
    }
	if(phone==phone1){
		$("#ChangePhoneTips").html("你的电话未修改");
		return false;
	}

	// checkPhone(phone1);
	if($("#ChangePhoneTips").html().trim()==""){
		$.ajax({
			type : "POST",
			url : "servlet/UserServlet",
			data : "action=changePhone&userName=" +userName+"&phone="+phone1,
			success : function(result) {
				var result= result.trim();
				if(result=="success"){
					$("#ChangePhoneTips").css("color","blue");
					$("#ChangePhoneTips").html("你的新号码已保存");
					return true;
				}else{
					$("#ChangePhoneTips").html("电话号码修改失败");
					return false;
				}
			}
		});
	}	
}

// 验证电话是否已被注册了。
function checkPhone(phoneNum) {
	$.ajax({
		type : "POST",
		url : "servlet/RegisteServlet",
		data : "flag=phone&value=" + phoneNum,
		error : function(error) {
		},
		success : function(response) {
			var result = response.trim();
			if (result == "PhoneNumAvailable") {
			} else if (result == "HaveUsed") {
				$('#ChangePhoneTips').html("该号码已经被使用");
				return false;
			}
		}
	});
}